//
//  MSHeroManager.m
//  MagicSword
//
//  Created by Netease on 15/11/16.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSHeroManager.h"
#import "MSDBManager.h"
#import "MSWeaponManager.h"

@implementation MSHeroManager

+ (id)shareManager {
    static MSHeroManager *heroMgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        heroMgr = [[MSHeroManager alloc] init];
    });
    return heroMgr;
}

- (id)init {
    self = [super init];
    if (self) {
        [self loadHeroArray];
    }
    return self;
}

- (void)loadHeroArray {
    _heroArray = [NSMutableArray array];
    NSArray *heroArray = [[MSDBManager shareManager] loadDBHeroArray];
    
    if (heroArray.count == 0) {
        MSHeroInfo *hero = [MSHeroManager createHeroWithModel:MSHeroModel_A];
        [_heroArray addObject:hero];
//        MSHeroInfo *hero1 = [MSHeroManager createHeroWithModel:MSHeroModel_B];
//        [_heroArray addObject:hero1];
//        MSHeroInfo *hero2 = [MSHeroManager createHeroWithModel:MSHeroModel_C];
//        [_heroArray addObject:hero2];
//        MSHeroInfo *hero3 = [MSHeroManager createHeroWithModel:MSHeroModel_D];
//        [_heroArray addObject:hero3];
//        MSHeroInfo *hero4 = [MSHeroManager createHeroWithModel:MSHeroModel_E];
//        [_heroArray addObject:hero4];
    } else {
        for (MSHeroInfo *info in heroArray) {
            [_heroArray addObject:[MSHeroManager createHeroWithModel:info.type WithOldInfo:info]];
        }
    }
}

- (NSArray *)getAllUserHeroArray {
    return _heroArray;
}

- (void)saveHeroArray {
    [[MSDBManager shareManager] saveDBHeroArray:_heroArray];
}

- (NSArray *)getFightingHeroArray {
    NSMutableArray *fightHero = [NSMutableArray array];
    for (MSHeroInfo *info in _heroArray) {
        if (info.isInFight) {
            [fightHero addObject:info];
        }
    }
    return fightHero;
}

// 卸载背包
- (BOOL)removeHeroWeapon:(MSHeroInfo *)hero weapon:(MSWeaponInfo *)weapon {
    if ([hero.weaponList indexOfObject:weapon] == NSNotFound) {
        [ProgressHUD showError:@"操作失败"];
        return NO;
    }
    [hero.weaponList removeObject:weapon];
    return YES;
}
// 增加
- (BOOL)addHeroWeapon:(MSHeroInfo *)hero weapon:(MSWeaponInfo *)weapon {
    if (hero.weaponList.count >= 3) {
        [ProgressHUD showError:@"该英雄背包已满"];
        return NO;
    }
    
    // 装备类型
    if ((int)hero.profession != (int)weapon.type) {
        [ProgressHUD showError:@"该英雄不能使用这个类型的武器"];
        return NO;
    }
    
    [hero.weaponList addObject:weapon];
    return YES;
}

//------------------------------------------------------------------------------------
#pragma mark - ++++++++++++++

+ (MSHeroInfo *)createHeroWithModel:(MSHeroModel)model {
    MSHeroInfo *hero = [[MSHeroInfo alloc] init];
    
    hero.type = model;
    switch (model) {
        case MSHeroModel_A:
            // 初始化内容
            hero.level = 1;
            hero.MaxHP = 40;
            hero.Attack = 12;
            hero.Experience = 0;
            hero.DoubleHit = 30;
            hero.Vampire = 0;
            hero.MissValue = 0;
            hero.isInFight = YES;
            hero.weaponList = [@[[MSWeaponManager createWeaponWithModel:MSWeaponModel_1]] mutableCopy];
//            hero.weaponList = [@[[MSWeaponManager createWeaponWithModel:MSWeaponModel_1],[MSWeaponManager createWeaponWithModel:MSWeaponModel_2],[MSWeaponManager createWeaponWithModel:MSWeaponModel_3]] mutableCopy];
            break;
            
        case MSHeroModel_B:
            // 初始化内容
            hero.level = 2;
            hero.MaxHP = 55;
            hero.Attack = 15;
            hero.Experience = 0;
            hero.DoubleHit = 0;
            hero.Vampire = 3;
            hero.MissValue = 0;
            hero.weaponList = [@[[MSWeaponManager createWeaponWithModel:MSWeaponModel_4]] mutableCopy];
//            hero.weaponList = [@[[MSWeaponManager createWeaponWithModel:MSWeaponModel_4],[MSWeaponManager createWeaponWithModel:MSWeaponModel_5],[MSWeaponManager createWeaponWithModel:MSWeaponModel_6]] mutableCopy];
            break;
            
        case MSHeroModel_C:
            // 初始化内容
            hero.level = 2;
            hero.MaxHP = 50;
            hero.Attack = 13;
            hero.Experience = 0;
            hero.DoubleHit = 0;
            hero.Vampire = 0;
            hero.MissValue = 20;
            hero.weaponList = [@[[MSWeaponManager createWeaponWithModel:MSWeaponModel_7]] mutableCopy];
//            hero.weaponList = [@[[MSWeaponManager createWeaponWithModel:MSWeaponModel_7],[MSWeaponManager createWeaponWithModel:MSWeaponModel_8],[MSWeaponManager createWeaponWithModel:MSWeaponModel_9]] mutableCopy];
            break;
            
        case MSHeroModel_D:
            // 初始化内容
            hero.level = 3;
            hero.MaxHP = 70;
            hero.Attack = 17;
            hero.Experience = 0;
            hero.DoubleHit = 10;
            hero.Vampire = 0;
            hero.MissValue = 10;
            hero.weaponList = [@[[MSWeaponManager createWeaponWithModel:MSWeaponModel_10]] mutableCopy];
//            hero.weaponList = [@[[MSWeaponManager createWeaponWithModel:MSWeaponModel_10],[MSWeaponManager createWeaponWithModel:MSWeaponModel_11],[MSWeaponManager createWeaponWithModel:MSWeaponModel_12]] mutableCopy];
            break;
            
        case MSHeroModel_E:
            // 初始化内容
            hero.level = 5;
            hero.MaxHP = 100;
            hero.Attack = 25;
            hero.Experience = 0;
            hero.DoubleHit = 0;
            hero.Vampire = 0;
            hero.MissValue = 0;
            hero.weaponList = [@[[MSWeaponManager createWeaponWithModel:MSWeaponModel_13]] mutableCopy];
//            hero.weaponList = [@[[MSWeaponManager createWeaponWithModel:MSWeaponModel_13],[MSWeaponManager createWeaponWithModel:MSWeaponModel_14],[MSWeaponManager createWeaponWithModel:MSWeaponModel_15]] mutableCopy];
            break;
        default:
            break;
    }
    
    hero.HP = hero.MaxHP;
    return [self createHeroWithModel:model WithOldInfo:hero];
}

+ (MSHeroInfo *)createHeroWithModel:(MSHeroModel)model WithOldInfo:(MSHeroInfo *)hero {
    SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"avatars"];
    hero.type = model;
    switch (model) {
        case MSHeroModel_A:
            // 不变内容
            hero.name = @"轩宇";
            hero.profession = MSHeroProfession_Sword;
            hero.professionName = [self getProfessionName:hero.profession level:hero.level];
            hero.avatarTexture = [atlas textureNamed:@"HeroModel_A"];
            hero.bodyTexture = [atlas textureNamed:@"HeroModel_A_Body"];
            hero.mapImage = @"MapHeroModel_A";
            hero.moveDistance = 4;
            hero.attackDistance = MSAttackDistanceType_A;
            break;
        case MSHeroModel_B:
            // 不变内容
            hero.name = @"吴奎";
            hero.profession = MSHeroProfession_Axe;
            hero.professionName = [self getProfessionName:hero.profession level:hero.level];
            hero.avatarTexture = [atlas textureNamed:@"HeroModel_B"];
            hero.bodyTexture = [atlas textureNamed:@"HeroModel_B_Body"];
            hero.mapImage = @"MapHeroModel_B";
            hero.moveDistance = 3;
            hero.attackDistance = MSAttackDistanceType_A;
            break;
        case MSHeroModel_C:
            // 不变内容
            hero.name = @"云翼";
            hero.profession = MSHeroProfession_Spear;
            hero.professionName = [self getProfessionName:hero.profession level:hero.level];
            hero.avatarTexture = [atlas textureNamed:@"HeroModel_C"];
            hero.bodyTexture = [atlas textureNamed:@"HeroModel_C_Body"];
            hero.mapImage = @"MapHeroModel_C";
            hero.moveDistance = 5;
            hero.attackDistance = MSAttackDistanceType_A;
            break;
        case MSHeroModel_D:
            // 不变内容
            hero.name = @"天泉";
            hero.profession = MSHeroProfession_Magic;
            hero.professionName = [self getProfessionName:hero.profession level:hero.level];
            hero.avatarTexture = [atlas textureNamed:@"HeroModel_D"];
            hero.bodyTexture = [atlas textureNamed:@"HeroModel_D_Body"];
            hero.mapImage = @"MapHeroModel_D";
            hero.moveDistance = 4;
            hero.attackDistance = MSAttackDistanceType_C;
            break;
        case MSHeroModel_E:
            // 不变内容
            hero.name = @"娄肖";
            hero.profession = MSHeroProfession_Bow;
            hero.professionName = [self getProfessionName:hero.profession level:hero.level];
            hero.avatarTexture = [atlas textureNamed:@"HeroModel_E"];
            hero.bodyTexture = [atlas textureNamed:@"HeroModel_E_Body"];
            hero.mapImage = @"MapHeroModel_E";
            hero.moveDistance = 4;
            hero.attackDistance = MSAttackDistanceType_B;
            break;
            
        default:
            break;
    }
    return hero;
}

+ (NSString *)getProfessionName:(MSHeroProfession)pro level:(NSInteger)level {
    NSArray *SwordNames = @[@"剑士", @"圣剑士", @"御剑士"];
    NSArray *AxeNames   = @[@"战士", @"狂战士", @"魔战士"];
    NSArray *SpearNames = @[@"骑士", @"圣骑士", @"御骑士"];
    NSArray *MagicNames = @[@"法师", @"玄法师", @"圣法师"];
    NSArray *BowNames = @[@"弓箭手", @"射手", @"神射士"];
//    NSArray *DragonNames = @[@"？？", @"龙骑士", @"烈狱骑士"];
    
    int index = 0;
    if (level >= 30) {
        index = 2;
    } else if (level >= 15) {
        index = 1;
    }
    NSString *name = @"";
    switch (pro) {
        case MSHeroProfession_Sword:
            name = SwordNames[index];
            break;
        case MSHeroProfession_Axe:
            name = AxeNames[index];
            break;
        case MSHeroProfession_Spear:
            name = SpearNames[index];
            break;
        case MSHeroProfession_Magic:
            name = MagicNames[index];
            break;
        case MSHeroProfession_Bow:
            name = BowNames[index];
            break;
//        case MSHeroProfession_Dragon:
//            name = DragonNames[index];
            break;
            
        default:
            break;
    }
    
    return name;
}

@end
